/**
 * 
 */
package com.alphasystem.commerce.model.message;

import static javax.persistence.EnumType.STRING;
import static javax.persistence.InheritanceType.JOINED;

import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.alphasystem.commerce.common.model.translation.TranslationInfo;
import com.alphasystem.commerce.common.model.translation.TranslationInfoSupport;
import com.alphasystem.commerce.model.message.folder.MessageFolder;

/**
 * @author sali
 * 
 */
@Entity
@Table(name = "MESSAGE")
@Inheritance(strategy = JOINED)
public class Message extends TranslationInfoSupport {

	private static final long serialVersionUID = -7498316547168380666L;

	protected String code;

	protected MessageFolder folder;

	protected MessageType type;

	protected transient String message;

	public Message() {
		this(TEMP_ID);
	}

	public Message(String id) {
		super(id);
		type = MessageType.INFO;
	}

	/**
	 * @return
	 */
	@Column(length = 20, nullable = false, unique = true)
	public String getCode() {
		return code;
	}

	/**
	 * @return the folder
	 */
	@ManyToOne
	@JoinColumn(name = "FOLDER_ID", nullable = false)
	public MessageFolder getFolder() {
		return folder;
	}

	@Transient
	public String getMessage() {
		Locale locale = Locale.getDefault();
		if (message == null || !locale.equals(currentLocale)) {
			TranslationInfo ti = getCurrent();
			if (ti == null) {
				// TODO: handle NULL
			}
			message = ti.getContent();
		}
		return message;
	}

	/**
	 * @return
	 */
	@Column(name = "MSG_TYPE", length = 15, nullable = false)
	@Enumerated(STRING)
	public MessageType getType() {
		return type;
	}

	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @param folder
	 *            the folder to set
	 */
	public void setFolder(MessageFolder folder) {
		this.folder = folder;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setType(MessageType type) {
		this.type = type;
	}

}
